package plgbasev0

/**
基础生产者
*/

import (
	"dgo/goutils/utils"
	"gitee.com/ymofen/supervalue"
)

type ProjBaseProduce struct {
	ProjBasePlugin

	out_ex_plugid string
	out_ex        interface{}
	out_sc        *utils.SubscribeCenter
	out_topic     string
	out_tag       interface{}
}

func (this *ProjBaseProduce) SetPlugID(val string) {
	this.PlugId = val
	if len(this.out_topic) == 0 {
		this.out_topic = val
	}
}

func (this *ProjBaseProduce) SetConsumeSubscriber(val *utils.SubscribeCenter) {
	this.out_sc = val
}

func (this *ProjBaseProduce) SetOutex(val interface{}) {
	this.out_ex = val
	if val == nil {
		return
	}
	if sc, ok := val.(*utils.SubscribeCenter); ok { // 消息发布
		this.SetConsumeSubscriber(sc)
	} else if intf, ok := val.(IGetSubscriber); ok {
		this.SetConsumeSubscriber(intf.GetSubscriber())
	}
}

func (this *ProjBaseProduce) OutputBufferTag(buffer []byte, tag interface{}) {
	if this.ReleaseFlag == 1 {
		this.out_sc = nil
		//this.out_fn = nil
		return
	}

	/**
	  数据分发
	*/
	sc := this.out_sc
	if sc != nil {
		sc.DispatchData(this.out_topic, this, buffer, tag)
	}

}

func (this *ProjBaseProduce) OutputBufferEx(buffer []byte, sender interface{}, tag interface{}) {
	if this.ReleaseFlag == 1 {
		this.out_sc = nil
		return
	}

	/**
	  数据分发
	*/
	sc := this.out_sc
	if sc != nil {
		sc.DispatchData(this.out_topic, sender, buffer, tag)
	}

}

func (this *ProjBaseProduce) OutputBuffer(buffer []byte) {
	if this.ReleaseFlag == 1 {
		this.out_sc = nil
		return
	}

	/**
	  数据分发
	*/
	sc := this.out_sc
	if sc != nil {
		sc.DispatchData(this.out_topic, this, buffer, this.out_tag)
	}

}

func (this *ProjBaseProduce) SetStatusEx(val interface{}) {
	this.status_ex = val
	if val == nil {
		return
	}
}

/*
*

	 {
	   "output":{
	      "id":"castex",    // 可以是一个订阅中心, 也可以是一个实现了consumerfun 的消费者
	      "topic":"xxx"    // 如果不设定 分发时 使用plugid作为channelid
	      "tag":"xxx"      // 不设定为空
	   },
	  "status_o":{
	      "id":"ex",
	      "topic":"xxxx"
	   }
	}
*/
func (this *ProjBaseProduce) ReloadOuptputConf(conf *wrapper.SuperValue) (err error) {

	conf.CheckStrFunc("output.id", func(strv string) {
		this.out_ex_plugid = strv
		ex := FindPluginByID(strv)
		this.SetOutex(ex)
	})
	conf.CheckStrFunc("output.topic", func(strv string) {
		this.out_topic = strv
	})
	conf.CheckExistsStrFunc("output.tag", func(strv string) {
		this.out_tag = strv
	})
	return
}
